package com.wuhaha.a1911_demo.utlis;

import android.content.ComponentName;
import android.util.Log;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.navigation.ActivityNavigator;
import androidx.navigation.NavController;
import androidx.navigation.NavGraph;
import androidx.navigation.NavGraphNavigator;
import androidx.navigation.NavigatorProvider;
import androidx.navigation.fragment.FragmentNavigator;

import com.wuhaha.a1911_demo.MainActivity;
import com.wuhaha.a1911_demo.model.Destination;
import com.wuhaha.a1911_demo.navigator.FixFragmentNavigator;

import java.util.HashMap;
import java.util.Iterator;

public
/**
 * Create date: On 2022/8/15
 */
class NavGraphBuilder {
    public static void build(NavController controller, MainActivity mainActivity, int id){
        //nav提供者
        NavigatorProvider provider = controller.getNavigatorProvider();
        //获取fragment或者activity的Nav
        FragmentNavigator fragmentNavigator = provider.getNavigator(FragmentNavigator.class);
        ActivityNavigator activityNavigator = provider.getNavigator(ActivityNavigator.class);

        NavGraph navGraph = new NavGraph(new NavGraphNavigator(provider));
        //获取按钮json对象
        HashMap<String, Destination> destConfig = AppConfig.getsDestConfig();
        //循环所有值
        for (Destination value : destConfig.values()) {
            //设置起始页
            Log.i("TAG---", "build: "+value.isAsStarter());
            if(value.isIsFragment()){
                //新建fragmentNav
                FragmentNavigator.Destination destination = fragmentNavigator.createDestination();
                //赋值
                destination.setClassName(value.getClassName());
                destination.setId(value.getId());
                //添加深度链接
                destination.addDeepLink(value.getPageUrl());
                //添加
                navGraph.addDestination(destination);
            }else{
                ActivityNavigator.Destination destination = activityNavigator.createDestination();
                //赋值
                destination.setId(value.getId());
                //添加深度链接
                destination.addDeepLink(value.getPageUrl());
                destination.setComponentName(new ComponentName(AppGlobals.getApplication().getPackageName(),value.getClassName()));
                //添加
                navGraph.addDestination(destination);
            }
            //是否起始页
            if(value.isAsStarter()){

                navGraph.setStartDestination(value.getId());
            }
        }
        //控制器设置navGraph
        controller.setGraph(navGraph);

    }
}
